package com.mayflygeek.mayflyask.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.validation.constraints.Past;
import javax.validation.constraints.Pattern;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.mayflygeek.mayflyask.util.UserUtils;
/**
 * @author 蝈蝈 
 * @email guodefu909@126.com
 * @team 蜉蝣极客
 * @link www.mayflygeek.com
 * 实体类
 */
@Entity
@Table(name = "mayflyask_user")
public class User implements Serializable{
	
	private Logger log = LoggerFactory.getLogger(getClass());
	
	private static final long serialVersionUID = 1L;
	private Long id;
	private String userName;
    private String nickName;
    private String password;
    private String confirmPassword;
    private String email;
    private String salt;
    private String telephone;
    private Date birth;
    private Integer sex;
    private Boolean rememberMe;
    private Role role;
    
    private Integer status;
    private Date updatedTime;
    private Long updater;
    private Date createdTime;
    private Long creater;
    
    @Id
	@GeneratedValue(strategy = GenerationType.IDENTITY )
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
    
    @NotBlank(message = "{User.userName.NotBlank}")
	@Column(name = "user_name",unique = true,length = 20,nullable = false)
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName == null ? null : userName.trim();
    }

    @NotBlank(message = "{User.nickName.NotBlank}")
	@Column(name = "nick_name",length = 20,nullable = false)
    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName == null ? null : nickName.trim();
    }

    @JsonIgnore
    @Pattern(regexp = "^[a-zA-Z]\\w{5,17}$", message = "{User.password.Pattern}")
    @Column(length = 40,nullable = false)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }
    
    @Transient
    public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	@Column(length = 20)
    @Email(message="{User.email.Email}")
    public String getEmail() {
    	return email;
    }
    
    public void setEmail(String email) {
    	this.email = email == null ? null : email.trim();
    }
    
    @NotBlank(message = "{User.salt.NotBlank}")
	@Column(name = "salt",length = 8,nullable = false)
    public String getSalt() {
        return salt;
    }
    
    public void setSalt(String salt) {
        this.salt = salt == null ? null : salt.trim();
    }

    @Column(length = 20)
    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone == null ? null : telephone.trim();
    }
    
    @JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
    @Past(message="{User.birth.Past}")
    @Temporal(TemporalType.DATE)
    public Date getBirth() {
		return birth;
	}

	public void setBirth(Date birth) {
		this.birth = birth;
	}

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }
    
	@Transient
	public Boolean getRememberMe() {
		return rememberMe;
	}

	public void setRememberMe(Boolean rememberMe) {
		this.rememberMe = rememberMe;
	}

	@ManyToOne
    @JoinColumn(name="role_id")
	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}
	
	
	
	
	/*====================公共部分可复用 Start=====================*/
	
	@Column(nullable = false)
	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    @Column(name = "updated_time", nullable = false)
	public Date getUpdatedTime() {
		return updatedTime;
	}

	public void setUpdatedTime(Date updatedTime) {
		this.updatedTime = updatedTime;
	}

	@Column(nullable = false)
	public Long getUpdater() {
		return updater;
	}

	public void setUpdater(Long updater) {
		this.updater = updater;
	}

	@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
	@Column(name = "created_time",nullable = false, updatable = false)
	public Date getCreatedTime() {
		return createdTime;
	}

	public void setCreatedTime(Date createdTime) {
		this.createdTime = createdTime;
	}

	@Column(nullable = false, updatable = false)
	public Long getCreater() {
		return creater;
	}

	public void setCreater(Long creater) {
		this.creater = creater;
	}
	
	@PrePersist
	public void prePersist() {
		log.info("prePersist");
		if (updater!=0L) {
			this.updater = UserUtils.getSessionUser().getId();
		}
		this.updatedTime = new Date();
		this.creater = this.updater;
		this.createdTime = this.updatedTime;
		status = status == null ? 1 : status;
	}

	@PreUpdate
    public void preUpdate(){
		if (updater!=0L) {
			this.updater = UserUtils.getSessionUser().getId();
		}
        this.updatedTime = new Date();
        status = status == null ? 1 : status;
	}
	
	/*====================公共部分可复用 End=====================*/

    
}